import '../models/order.dart';
import 'http_client.dart';

class OrderService {
  final HttpClient _httpClient = HttpClient();

  /// 获取用户订单列表
  Future<OrderListResponse> getUserOrders({
    int pageNum = 1,
    int pageSize = 10,
    required String userId,
  }) async {
    try {
      print('OrderService: 准备调用订单接口');
      print('OrderService: 参数 - pageNum: $pageNum, pageSize: $pageSize, userId: $userId');
      
      final response = await _httpClient.get(
        '/pond/order/list',
        queryParameters: {
          'pageNum': pageNum,
          'pageSize': pageSize,
          'userId': userId,
        },
      );

      print('OrderService: 接口调用成功，返回数据: $response');
      return OrderListResponse.fromJson(response);
    } catch (e) {
      print('OrderService: 接口调用失败: $e');
      throw Exception('获取订单列表失败: $e');
    }
  }

  /// 获取订单详情
  Future<Order> getOrderDetail(String orderId) async {
    try {
      final response = await _httpClient.get('/pond/order/$orderId');
      
      if (response.data['code'] == 200) {
        return Order.fromJson(response.data['data']);
      } else {
        throw Exception(response.data['msg'] ?? '获取订单详情失败');
      }
    } catch (e) {
      throw Exception('获取订单详情失败: $e');
    }
  }
}